<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Systemset_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function arrays($key) {
        $data = array(
            'sys_type' => array("1" => "充值手续费", "2" => "理财管理费", 3 => "提现手续费", 4 => 'VIP服务费', 5 => '风险准备金'),
        );

        if ($key && isset($data[$key])) {
            return $data[$key];
        } else {
            return array();
        }
    }

    /**
     * Publish system set
     *
     * @access	public
     * @param   array
     * @return  string
     */
    function add($data = array()) {
        $this->db->insert('rsh_system_set', $data);
        return $this->db->insert_id();
    }

    /**
     * Update system set
     *
     * @param  boolean
     * @param  array
     * @return string
     */
    function update($id, $data = array()) {
        $this->db->where('id', $id);
        $this->db->update('rsh_system_set', $data);
        return $this->db->affected_rows();
    }

    /**
     * Check system set
     *
     * @access	public
     * @return	bool
     */
    function check_systemset() {
        $this->db->select('id');
        $query = $this->db->get('rsh_system_set');
        $query->num_rows();
        if ($query->num_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * Get system set
     *
     * @access	public
     * @return	array
     */
    function get_systemset($where = array(), $select = '*') {
        $this->db->select($select);
        if ($where) {
            $this->db->where($where);
        }
        $this->db->limit(1);
        $query = $this->db->get('rsh_system_set');
        return $query->row_array();
    }

    function systemsetlist($data) {
        $page_size = $data['page_size'];
        $page_start = $data['page_start'];

        $sql_result = "select * from rsh_system_set";
        $_where = " where 1=1  and is_del =0 ";
        if (isset($data['where']['ftype']) && $data['where']['ftype']) {
            $_where .= " and ftype = " . $data['where']['ftype'];
        }

        $sql_result .= $_where;
        $sql_result .= ' order by id desc ';
        if (isset($data['limit']) && $data['limit'] == 'all') {
            $query = $this->db->query($sql_result);
            $data['sys_list'] = $query->result_array();
            return $data;
        } else {
            $sql_result .= " limit $page_start, $page_size";
            $query = $this->db->query($sql_result);
            $data['sys_list'] = $query->result_array();
        }


        $sql_count = "select count(*) as count from rsh_system_set";
        $sql_count .= $_where;
        $query = $this->db->query($sql_count);
        $data['sys_count'] = $query->row()->count;
        return $data;
    }

    function show_tables() {
        $key = 'rsh_show_tables_list';
        //$this->mycache->delete($key);
        $data['table_list'] = $this->mycache->get_array($key);
        if ($data['table_list'] == false) {
            $sql = "SHOW TABLE STATUS FROM `zhsjr` WHERE ENGINE IS NOT NULL";
            $query = $this->db->query($sql);
            $data['table_list'] = $query->result_array();
            $this->mycache->set_array($key, $data['table_list']);
        }
        return $data;
    }

    function desc_tables($table) {
        $sql = "DESC " . $table;
        $query = $this->db->query($sql);
        $data['table_list'] = $query->result_array();
        return $data;
    }

}
